from machine_lib import * 
s = login()
## get promising alphas to improve in the next order
fo_tracker = get_alphas("01-21", "01-26", 0.8, 0.7, "USA", 500, "track", delay=1, name="USA_fo_model165")

# 12-9 to 12-11 TWN_fo_fundmental6
print(len(fo_tracker['next']))
print(len(fo_tracker['decay']))# + fo_tracker['decay']

fo_layer = prune(fo_tracker['decay']+fo_tracker['next'], 'usa', ' ', 1)
so_alpha_list = []
group_ops = group_ops = ["group_neutralize", "group_rank", "group_scale"]#, "group_normalize", "group_zscore"
for region, couples in fo_layer.items():
    for expr, decay in couples:
        for alpha in get_group_second_order_factory([expr], group_ops, region):
            so_alpha_list.append((alpha,decay))

print(len(so_alpha_list))
print(so_alpha_list[:20])

def shuffle(alpha_list, interval):
    n = len(alpha_list)
    shuffled_list = []
    for i in range(interval):
        shuffled_list.append(alpha_list[i:n:interval])
        flattened_list = [item for sublist in shuffled_list for item in sublist]
    return flattened_list

so_alpha_list = shuffle(so_alpha_list, 1)
so_pools = load_task_pool(so_alpha_list, 10, 10)
print(so_pools[0])
print(len(so_pools))

# multi_simulate(so_pools, neut='SUBINDUSTRY', region='GLB', universe='TOP3000', start=0, name="GLB_so_md109", delay=1)
multi_simulate(so_pools, neut='SUBINDUSTRY', region='USA', universe='TOP3000', start=0, name="USA_so_md165", delay=1)
# multi_simulate(so_pools, neut='SECTOR', region='USA', universe='TOP3000', start=0, name="USA_so_fd14_SEC", delay=1)